package com.stylefeng.guns.modular.bank.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.core.common.exception.BizExceptionEnum;
import com.stylefeng.guns.core.exception.GunsException;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.modular.bank.service.IBankService;
import com.stylefeng.guns.modular.system.dao.BankMapper;
import com.stylefeng.guns.modular.system.model.Bank;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author stylefeng123
 * @since 2018-07-02
 */
@Service
public class BankServiceImpl extends ServiceImpl<BankMapper, Bank> implements IBankService{


    /**
     * 添加銀行信息
     * @param list
     * @throws GunsException
     */
    @Override
    public void adds(List<Bank> list) throws GunsException{
        for (Bank bank:list){
            if (StringUtils.isBlank(bank.getPlatform())){
                throw new GunsException (BizExceptionEnum.BUSINSS_ERROR,"报表所在的平台不能为空");
            }
            if (StringUtils.isBlank(bank.getRevtime().toString())){
                throw new GunsException (BizExceptionEnum.BUSINSS_ERROR,"报表统计时间不能为空");
            }
        }
        baseMapper.adds(list);
    }

    /**
     * 获取解冻金额
     * @param platform
     * @param beginDate
     * @param endDate
     * @return
     * @throws GunsException
     */
    @Override
    public Double thawMoney(String platform, Date beginDate, Date endDate) throws GunsException {
        Bank bank = baseMapper.thawMoney(platform,beginDate,endDate);
        if (bank==null){
            return 0.0;
        }
        return bank.getBalance();
    }

    /**
     * 获取冻结金额
     * @param platform
     * @param beginDate
     * @param endDate
     * @return
     * @throws GunsException
     */
    @Override
    public Double freezeMoney(String platform, Date beginDate, Date endDate) throws GunsException {
        Bank bank = baseMapper.freezeMoney(platform,beginDate,endDate);
        if (bank==null){
            return 0.0;
        }
        return bank.getBalance();
    }

    /**
     * 查询某天的银行余额
     * @param revtime
     * @return
     */
    @Override
    public Double searchOneDayBalance(String platform, Date revtime) {
        Bank bank = baseMapper.getOneDayBalance(platform,revtime);
        if (bank==null)
            return 0.0;
        return bank.getBalance();
    }

    /**
     * 描述:导出银行余额表
     * 创建人: xiang
     * 日期: 2018/7/31
     * 时间: 14:09
     */
    @Override
    public void exportBank( String revtime,Map<String, Object> map){
        String platform=ShiroKit.getUser().getDeptcode();
        //查询入款卡
        List<Bank> ruList= this.selectList(new EntityWrapper<Bank>()
                .eq("platform",platform)
                .eq("banktype",Bank.Enum_BankType.入款银行.value.intValue())
                .eq("platform",platform)
                .eq("revtime",revtime)
        );
        //入款合计金额
        Bank rusumbank= baseMapper.getRusumbank(platform,revtime,Bank.Enum_BankType.入款银行.value.intValue(),Bank.Enum_Status.冻结状态.value);

        //查询出款卡 中转卡
        List<Bank> chuList= this.selectList(new EntityWrapper<Bank>()
                .eq("platform",platform)
                .ge("banktype",Bank.Enum_BankType.出款银行.value.intValue())
                .eq("revtime",revtime)
        );
        //入款合计金额
        Bank chusumbank=  baseMapper.getChusumbank(platform,revtime,Bank.Enum_BankType.出款银行.value.intValue(),Bank.Enum_Status.冻结状态.value);

        //查询冻结卡
        List<Bank> djList= this.selectList(new EntityWrapper<Bank>()
                .eq("platform",platform)
                .eq("status",Bank.Enum_Status.冻结状态.value.intValue())
                .eq("revtime",revtime)
        );
        //冻结卡合计
        Bank djsumbank= this.selectOne(new EntityWrapper<Bank>().setSqlSelect(" SUM(balance)  as balance ")
                .eq("platform",platform)
                .eq("status",Bank.Enum_Status.冻结状态.value.intValue())
                .eq("revtime",revtime)
        );

        //底部合计
        map.put("bankky",chusumbank.getBalance()+rusumbank.getBalance());             //银行可用金额 入款卡余额+出款余额
        map.put("bankdj",chusumbank.getFreezeBalance()+rusumbank.getFreezeBalance());         //银行冻结金额 入款卡冻结金额+出款卡冻结金额
        map.put("bankzj",chusumbank.getBalance()+rusumbank.getBalance()+
                chusumbank.getFreezeBalance()+rusumbank.getFreezeBalance());                  //银行总余额   银行可用金额+银行冻结金额
        //合并列
        List<Map<String,Object>> banklist= new ArrayList<Map<String, Object>>();
        getbanklist(banklist,ruList,"ruk");
        getbanklist(banklist,chuList,"chuk");
        getbanklist(banklist,djList,"djk");
        map.put("rusumbank",rusumbank);
        map.put("chusumbank",chusumbank);
        map.put("djsumbank",djsumbank);
        map.put("bankList",banklist);

        //TODO 未完成 要修改bank表结构

    }




    /**
     * 描述:组装导出数据
     * 创建人: xiang
     * 日期: 2018/8/1
     * 时间: 11:08
     */
    public void  getbanklist(List<Map<String,Object>> banklist ,List<Bank> list ,String str){
        for(int i=0;i<list.size();i++){
            Map<String, Object> bankmap=null;
            if(banklist.size()!=0 && banklist.size()>=list.size()){
                bankmap=banklist.get(i);
                bankmap.put(str+"username",list.get(i).getUsername());
                bankmap.put(str+"bankName",list.get(i).getBankName());
                if(list.get(i).getStatus().intValue()==Bank.Enum_Status.冻结状态.value){
                    bankmap.put(str+"djbalance",list.get(i).getBalance());
                }else {
                    bankmap.put(str+"balance",list.get(i).getBalance());
                }
                bankmap.put(str+"rsemarks",list.get(i).getRsemarks());
            }
            if(bankmap==null ){
                bankmap = getBankMap();
                bankmap.put(str+"username",list.get(i).getUsername());
                bankmap.put(str+"bankName",list.get(i).getBankName());
                if(list.get(i).getStatus().intValue()==Bank.Enum_Status.冻结状态.value){
                    bankmap.put(str+"djbalance",list.get(i).getBalance());
                }else {
                    bankmap.put(str+"balance",list.get(i).getBalance());
                }
                bankmap.put(str+"rsemarks",list.get(i).getRsemarks());
                banklist.add(bankmap);
            }


        }
    }

    /**
     * 描述:组装导出模板内容
     * 创建人: xiang
     * 日期: 2018/8/1
     * 时间: 11:04
     */
    public   Map<String, Object> getBankMap(){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("rukusername","");
        map.put("rukbankName","");
        map.put("rukbalance","");
        map.put("rukdjbalance","");
        map.put("rukrsemarks","");
        map.put("chukusername","");
        map.put("chukbankName","");
        map.put("chukbalance","");
        map.put("chukdjbalance","");
        map.put("chukrsemarks","");
        map.put("djkusername","");
        map.put("djkbankName","");
        map.put("djkbalance","");
        map.put("djkdjbalance","");
        map.put("djkrsemarks","");
        return  map;
    }



}
